#include "FoGGEDEF.h"
#include "GeneralVariable.h"
#include "GVContainer.h"

namespace _FOGGE_
{
	namespace _GDB_
	{
		GeneralVariable::GeneralVariable(::_FO_GDB::GVContainer* ptrGVContainer,::_FO_UTIL::Name name,::_FO_UTIL::Name type):
			m_nameName(name),m_nameType(type)
		{
			m_uiFlags=eGVF_Latch;
			m_bModified=false;			// set each time the gv is changed
			m_uiModificationCount=0;	// incremented each time the gv is changed
		
			m_strValue="";
			m_strLatchValue="";

			ptrGVContainer->Add(this);

		}
		GeneralVariable::~GeneralVariable(void)
		{
		}

		
		// updates an interpreted modules' version of a cvar
		void GeneralVariable::Update( float elapsedtime )
		{
			if(m_bModified)
			{// this gv is modified
				if(m_uiFlags&eGVF_Latch)
				{// this gv has latch value
					if( m_strValue != m_strLatchValue )
					{
					    m_strValue = m_strLatchValue;	

						ValueUpdated();
					}
				}
				m_bModified = false;
			}
		}
		
		
		void GeneralVariable::Set( LPCTSTR value )
		{

			if(m_strValue!=value)
			{
				if(m_uiFlags&eGVF_Latch)
				{
					if(m_strLatchValue==__T(""))
					{//this latch value is not initialized
						m_strLatchValue = value;
					}
				}
				
				m_strValue = value;

				m_bModified = true;			// set each time the gv is changed
				m_uiModificationCount++;	// incremented each time the gv is changed
			}
		}
		
		
		void GeneralVariable::CheckModify()
		{
			if(m_bModified)
			{// this gv is modified
				if(m_uiFlags&eGVF_Latch)
				{// this gv has latch value
					if( m_strValue != m_strLatchValue )
					{//override with latch value
						m_strValue = m_strLatchValue;
					}
				}
			}
		}
		
		
		LPCTSTR GeneralVariable::GetString()
		{
			CheckModify();

			return m_strValue.c_str();

		}

		::_FO_UTIL::Name GeneralVariable::GetName()
		{
			return m_nameName;
		}
		::_FO_UTIL::Name GeneralVariable::GetType()
		{
			return m_nameType;
		}
		
	}
}